package com.th.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.th.bean.Category;
import com.th.bean.Setmeal;
import com.th.bean.SetmealDish;
import com.th.common.R;
import com.th.dto.SetmealDto;
import com.th.service.CategoryService;
import com.th.service.SetmealDishService;
import com.th.service.SetmealService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping()
    @CacheEvict(value = "setmealCache",allEntries = true)
    @Transactional
    //添加套餐操作
    public R<String>  add(@RequestBody SetmealDto setmealDto){
        Setmeal setmeal = new Setmeal();
        if(setmealDto!=null) {
            BeanUtils.copyProperties(setmealDto, setmeal, "setmealDishs");
        }else {
            return R.error("添加失败");
        }
        setmealService.save(setmeal);
        //System.out.println("---------------"+setmeal);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmeal.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);

        return R.success("成功添加");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>();
        Page<SetmealDto> setmealDtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> slw = new LambdaQueryWrapper<>();
        slw.like(!StringUtils.isEmpty(name),Setmeal::getName,name);
        slw.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(setmealPage,slw);

        BeanUtils.copyProperties(setmealPage,setmealDtoPage);

        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> collect = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(collect);

        return R.success(setmealDtoPage);
    }

    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deleteTC(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return R.success("删除成功");
    }
      @PostMapping("/status/{status}")
      @CacheEvict(value = "setmealCache",allEntries = true)
      public R<String> updataTC(@PathVariable int status,Long[] ids){
        for (Long id : ids){
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("状态改变");
      }

      @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
   //   @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
      public R<List<Setmeal>> list(Setmeal setmeal){
          LambdaQueryWrapper<Setmeal> slw = new LambdaQueryWrapper<>();
          slw.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
          slw.eq(Setmeal::getStatus, setmeal.getStatus());

          List<Setmeal> list = setmealService.list(slw);
          return R.success(list);
      }

}
